# coding: utf-8

from lib.tools.type import RedisConnectType
from lib.redis.abstract import RedisAbstract


class RedisFacotry(RedisAbstract):
    """ redis连接器 """

    @staticmethod
    def create_client(host, port=6379, auth=None, db=0, logger=None):
        # from lib.redis.client import RedisClient
        # return RedisClient(host=host, port=port, auth=auth, db=db, logger=logger)
        return RedisFacotry(type=RedisConnectType.Client, info=(host, port, db), auth=auth, logger=logger)

    @staticmethod
    def create_cluster(nodes, auth=None, logger=None):
        # from lib.redis.cluster import RedisCluster
        # return RedisCluster(nodes=nodes, auth=auth, logger=logger)
        return RedisFacotry(type=RedisConnectType.Cluster, info=nodes, auth=auth, logger=logger)


    def __init__(self, type, info, auth=None, logger=None):
        super(RedisFacotry, self).__init__(logger)
        if type == RedisConnectType.Client:
            from lib.redis.client import RedisClient
            host, port, db = info
            self.factory = RedisClient(host, port=port, db=db, auth=auth, logger=logger)
        elif type == RedisConnectType.Cluster:
            from lib.redis.cluster import RedisCluster
            self.factory = RedisCluster(nodes=info, auth=auth, logger=logger)
        else:
            raise Exception('redis connect type error!')

    def ping(self):
        return self.factory.ping()

    def size(self):
        # self.logger.info('command SIZE')
        return self.factory.size()

    def ttl(self, name):
        # self.logger.info('command TTL %s' % name)
        return self.factory.ttl(name)

    def expire(self, name, time):
        self.logger.info('command EXPIRE %s %d' % (name, time))
        return self.factory.expire(name, time)

    def persist(self, name):
        self.logger.info('command PERSIST %s' % name)
        return self.factory.persist(name)

    def type(self, name):
        # self.logger.info('command TYPE %s' % name)
        return self.factory.type(name)

    def rename(self, name, new_name):
        return self.factory.rename(name, new_name)

    def renamenx(self, name, new_name):
        return self.factory.renamenx(name, new_name)

    def keys(self, pattern='*'):
        self.logger.info('command KEYS %s' % pattern)
        return self.factory.keys(pattern)

    def scan(self, cursor=0, match=None, count=None):
        self.logger.info('command SCAN %d MATCH %s COUNT %d' % (cursor, match, count))
        return self.factory.scan(cursor, match, count)

    def exists(self, name):
        self.logger.info('command EXISTS %s' % name)
        return self.factory.exists(name)

    def get(self, name):
        self.logger.info('command GET %s' % name)
        return self.factory.get(name)

    def set(self, name, value):
        self.logger.info('command SET %s %s' % (name, value))
        return self.factory.set(name, value)

    def getset(self, name, value):
        self.logger.info('command GETSET %s %s' % (name, value))
        return self.factory.getset(name, value)

    def delete(self, name):
        self.logger.info('command DEL %s' % name)
        return self.factory.delete(name)

    def hexists(self, name, key):
        self.logger.info('command HEXISTS %s %s' % (name, key))
        return self.hexists(name, key)

    def hall(self, name):
        self.logger.info('command HALL %s' % name)
        return self.factory.hall(name)

    def hget(self, name, key):
        self.logger.info('command HGET %s %s' % (name, key))
        return self.factory.hget(name, key)

    def hset(self, name, key, value):
        self.logger.info('command HSET %s %s %s' % (name, key, value))
        return self.factory.hset(name, key, value)

    def hmget(self, name, keys, *args):
        self.logger.info('command HMGET %s ... ...' % name)
        return self.factory.hmget(name, keys, *args)

    def hmset(self, name, dict):
        self.logger.info('command HMSET %s ...' % name)
        return self.factory.hmset(name, dict)

    def hdel(self, name, key):
        self.logger.info('command HDEL %s %s' % (name, key))
        return self.factory.hdel(name, key)


